<?php
namespace app\index\controller;
use app\common\controller\Base;
use app\common\model\CarModel;
use think\Session;
use think\Db;

class Car extends Base
{
	//管理员列表
    public function oemList()
    {
    	$this -> view -> assign('title', '厂家列表');
        $this -> view -> assign('keywords', 'PHP中文网教学系统');
        $this -> view -> assign('desc', '教学案例');
        
        
        //判断当前是不是admin用户
        //先通过session获取到用户登陆名
        $userName = Session::get('user_info.username');
        
        if ($userName == 'admin') {
            // $list = UserModel::where('delete_time',null)->select();  
            // $list = CarModel::all();
            $list = CarModel::where("pid",0)->select();

            //admin用户可以查看所有记录,数据要经过模型获取器处理
        } else {
            //为了共用列表模板,使用了all(),其实这里用get()符合逻辑,但有时也要变通
            //非admin只能看自己信息,数据要经过模型获取器处理
            $map['name'] = Session::get('user_info.oem');
            $list = CarModel::where($map)->select();
        }

        $this -> view -> count = sizeof($list);
        $this -> view -> assign('list', $list);
        //渲染管理员列表模板	
        return $this -> view -> fetch('oem_list');
    }

    public function oemAdd(){
    	return $this->view->fetch("oem_add");
    }

    public function checkOemName(){
    	$oem_name = trim($this->request->param("name"));
    	$status = 1;
        $message = '用户名可用';
        if (empty($oem_name)) {
        	$status = 0;
            $message = '用户名不能为空~~';
        }
        if (CarModel::where('pid',0)->get(['name'=> $oem_name])) {
            //如果在表中查询到该用户名
            $status = 0;
            $message = '用户名重复,请重新输入~~';
        }
        return ['status'=>$status, 'message'=>$message];
    }

    public function addOem(){
    	$data = $this->request->param();
        $status = 0;
        $message = '添加失败，注册信息不符合要求~~';

        // var_dump($data);
        $rule = [
            'oem|厂家' => "require|min:3|max:20",
        ];
        
        $result = $this -> validate($data, $rule);

        if ($result === true) {
            $userr = CarModel::get(['name' => $data["oem"]]);  //判断是否为注册过的用户名称
            if ($userr === null) {
                $user= CarModel::create(['name' => $data["oem"],"pid" => 0]);
                if ($user) {
                    $status = 1;
                    $message = '添加成功~~';
                }
            }else{
                $message = '该用户名称或者厂家已经注册~~';
            }
            
        }


        return ['status'=>$status, 'message'=>$message];
    }

    public function oemEdit(){
        $id = $this->request->param("id");
        $result = CarModel::get($id);

        $this->view->assign('res',$result->getData());
        return $this->view->fetch("oem_edit");
    }
    
    public function editOem(){
        
        $data = $this->request -> param();
        // return $data;
        
        $condition = ['id'=>$data['id']] ;
        $result = CarModel::update(['name' => $data["oem"]], $condition);
        
        if (true == $result) {
            return ['status'=>1, 'message'=>'更新成功'];
        } else {
            return ['status'=>0, 'message'=>'更新失败,请检查'];
        }

    }

    public function deleteOem(){
    	$oem_id = $this->request->param("id");
    	CarModel::update(['is_delete'=>0],['id'=> $oem_id]);
        CarModel::destroy($oem_id);
    }

     //恢复删除操作
    public function unDelete()
    {
        $map['delete_time'] = NULL;
        $map['is_delete'] = 1;
        CarModel::update($map,['is_delete'=>0]);
        // CarModel::update(['delete_time'=>NULL],['is_delete'=>0]);
    }


    //管理员列表
    public function brandList()
    {
    	$this -> view -> assign('title', '品牌列表');
        
        //判断当前是不是admin用户
        //先通过session获取到用户登陆名
        $userName = Session::get('user_info.username');
        $map['pid'] = 0;

        if ($userName == 'admin') {
            // $list = UserModel::where('delete_time',null)->select();  
            // $list = CarModel::all();
            $res = CarModel::where($map)->column('id,name');
        }else{
        	$map['name'] = Session::get('user_info.oem');
        	$res = CarModel::where($map)->column('id,name');
        }

        $list = [];
    	foreach ($res as $key => $value) {
    		$ress = CarModel::where(['pid' => $key])->select();
    		// $result[] = $ress;
    		// var_dump($ress);
    		foreach ($ress as $key2 => $value2) {
    			$list[] = [
    				"id" => $value2->id,
    				"name" => $value2->name,
    				"pid" => $value2->pid,
    				"pname" => $value,
    				"create_time" => $value2->create_time,
    				"update_time" => $value2->update_time
    			];
    		}
    	}

        $this -> view -> count = sizeof($list);
        $this -> view -> assign('list', $list);
        //渲染管理员列表模板	
        return $this -> view -> fetch('brand_list');
    }

    //管理员列表
    public function brandAdd()
    {	
    	$userName = Session::get('user_info.username');
        $map['pid'] = 0;
    	if ($userName == 'admin') {
            $list = CarModel::where($map)->select();
        }else{
        	$map['name'] = Session::get('user_info.oem');
        	$list = CarModel::where($map)->select();
        }

        $this -> view -> assign('list', $list);
    	return $this->view->fetch("brand_add");
	}


	public function checkBrandName(){
		$brand_name = trim($this->request->param("name"));
    	$status = 1;
        $message = '用户名可用';
        if (empty($brand_name)) {
        	$status = 0;
            $message = '用户名不能为空~~';
        }
        if (CarModel::get(['name'=> $brand_name])) {
            //如果在表中查询到该用户名
            $status = 0;
            $message = '用户名重复,请重新输入~~';
        }
        return ['status'=>$status, 'message'=>$message];
	}

	public function addBrand(){
		$data = $this->request-> param();
        $status = 0;
        $message = '添加失败，注册信息不符合要求~~';

        $userr = CarModel::get(['name' => $data["brand"]]);  //判断是否为注册过的用户名称
        if ($userr === null) {
            $user= CarModel::create(['name' => $data["brand"],"pid" =>$data["oem"]]);
            if ($user) {
                $status = 1;
                $message = '添加成功~~';
            }
        }else{
            $message = '该用户名称或者厂家已经注册~~';
        }

        return ['status'=>$status, 'message'=>$message];
	}


	public function brandEdit(){
		$userName = Session::get('user_info.username');
        $map['pid'] = 0;
    	if ($userName == 'admin') {
            $list = CarModel::where($map)->select();
        }else{
        	//如果是某个厂家的，只能看到自己的
        	$map['name'] = Session::get('user_info.oem');
        	$list = CarModel::where($map)->select();
        }

        $id = $this->request->param("id");
        $result = CarModel::get($id);

        $this->view->assign('user_info',$result->getData());
        $this->view->assign('list', $list);
		return $this->view->fetch("brand_edit");
	}

	public function editBrand(){
		
		$data = $this->request -> param();

        $condition = ['id'=>$data['id']] ;
        $result = CarModel::update($data, $condition);
        
        if (true == $result) {
            return ['status'=>1, 'message'=>'更新成功'];
        } else {
            return ['status'=>0, 'message'=>'更新失败,请检查'];
        }

	}


    public function hello(){

        // $res = Db::table('car_model')->where('is_delete',1)->select();
        $res = Db::query("select * from car_model where pid=0");
    	
        foreach ($res as $key => $value) {
            echo($key);
            echo("   ");
            echo($value["id"]);
            echo("\t");
            echo($value["name"]);
            echo("\t");
            echo($value["pid"]);
            echo("<br/>");
            echo("<br/>");
        };

        echo("<hr>");
        $ress = Db::query("select a.id as aid,a.name as aname,b.id as bid,b.name as bname "
            . "from car_model as a, car_model as b where a.pid=b.id and b.pid = 0");

        foreach ($ress as $key => $value) {
            # code...
            echo($key);
            echo("   ");
            echo($value["aid"]);
            echo("\t");
            echo($value["aname"]);
            echo("\t");
            echo($value["bname"]);
            echo("<br/>");
            echo("<br/>");
        };

        echo("<hr>");
        $resss = Db::query("select a.id as aid,a.name as aname,"
            ."FROM_UNIXTIME(a.create_time,'%Y年%m月%d日') as create_time,"
            ."FROM_UNIXTIME(a.update_time,'%Y年%m月%d日') as update_time,"
            ."b.id as bid,b.name as bname,"
            ."c.id as cid,c.name as cname "
            . "from car_model as a,car_model as b,car_model as c "
            . "where a.pid = b.id and b.pid = c.id");
        $resss = Db::query("select a.id as aid,a.name as aname,"
            ."FROM_UNIXTIME(a.create_time,'%Y年%m月%d日') as create_time,"
            ."FROM_UNIXTIME(a.update_time,'%Y年%m月%d日') as update_time,"
            ."b.id as bid,b.name as bname,"
            ."c.id as cid,c.name as cname "
            . "from car_model as a,car_model as b,car_model as c "
            . "where a.pid = b.id and b.pid = c.id and c.name = ?",["SVW"]);
        foreach ($resss as $key => $value) {
                # code...
                echo($key);
                echo("   ");
                echo($value["aid"]);
                echo("\t");
                echo($value["aname"]);
                echo("\t");
                echo($value["create_time"]);
                echo("\t");
                echo($value["update_time"]);
                echo("\t");
                echo($value["bid"]);
                echo("\t");
                echo($value["bname"]);
                echo("\t");
                echo($value["cid"]);
                echo("\t");
                echo($value["cname"]);
                echo("\t");
                echo("<br/>");
                echo("<br/>");
        };


    }


    //管理员列表
    public function modelList()
    {
        $this -> view -> assign('title', '品牌列表');
        
        //判断当前是不是admin用户
        //先通过session获取到用户登陆名
        $userName = Session::get('user_info.username');
        
        if ($userName == 'admin') {
            // $list = UserModel::where('delete_time',null)->select();  
            // $list = CarModel::all();
            // $res = Db::query("select a.id as aid,a.name as aname,"
            // ."FROM_UNIXTIME(a.create_time,'%Y年%m月%d日') as create_time,"
            // ."FROM_UNIXTIME(a.update_time,'%Y年%m月%d日') as update_time,"
            // ."b.id as bid,b.name as bname,"
            // ."c.id as cid,c.name as cname "
            // . "from car_model as a,car_model as b,car_model as c "
            // . "where a.pid = b.id and b.pid = c.id");
            $res = Db::query("select a.id as aid,a.name as aname,a.is_delete,"
            ."FROM_UNIXTIME(a.create_time,'%Y年%m月%d日') as create_time,"
            ."FROM_UNIXTIME(a.update_time,'%Y年%m月%d日') as update_time,"
            ."b.id as bid,b.name as bname,"
            ."c.id as cid,c.name as cname "
            . "from car_model as a,car_model as b,car_model as c "
            . "where a.pid = b.id and b.pid = c.id and a.is_delete = ?",[1]);
        }else{
            $oemName = Session::get('user_info.oem');
            $res = Db::query("select a.id as aid,a.name as aname,"
            ."FROM_UNIXTIME(a.create_time,'%Y年%m月%d日') as create_time,"
            ."FROM_UNIXTIME(a.update_time,'%Y年%m月%d日') as update_time,"
            ."b.id as bid,b.name as bname,"
            ."c.id as cid,c.name as cname "
            . "from car_model as a,car_model as b,car_model as c "
            . "where a.pid = b.id and b.pid = c.id and c.name = ?",[$oemName]);
        }

        $list = [];
        foreach ($res as $key => $value) {
            $list[] = [
                "aid" => $value['aid'],
                "aname" => $value['aname'],
                "create_time" => $value['create_time'],
                "update_time" => $value['update_time'],
                "bid" => $value['bid'],
                "bname" => $value['bname'],
                "cid" => $value['cid'],
                "cname" => $value['cname']
            ];
        }

        $this -> view -> count = sizeof($list);
        $this -> view -> assign('list', $list);
        //渲染管理员列表模板 
        return $this -> view -> fetch('model_list');
    }

    public function modelAdd()
    {   
        $userName = Session::get('user_info.username');

        if ($userName == 'admin') {
            // $list = UserModel::where('delete_time',null)->select();  
            // $list = CarModel::all();
            $res_brand = Db::query("select a.id as aid,a.name as aname,"
            ."b.id as bid,b.name as bname "
            . "from car_model as a,car_model as b "
            . "where a.pid = b.id and b.pid = 0");
            $res_oem = Db::query("select a.id as aid,a.name as aname "
            . "from car_model as a "
            . "where a.pid = 0");

        }else{
            $oemName = Session::get('user_info.oem');
            $res_brand = Db::query("select a.id as aid,a.name as aname,"
            ."b.id as bid,b.name as bname "
            . "from car_model as a,car_model as b "
            . "where a.pid = b.id and b.pid = 0 and b.name = ?",[$oemName]);
            $res_oem = Db::query("select a.id as aid,a.name as aname "
            . "from car_model as a "
            . "where a.pid = 0 and a.name = ?",[$oemName]);
        }

        $list_oem = [];
        foreach ($res_oem as $key => $value) {
            $list_oem[] = [
                "aid" => $value['aid'],
                "aname" => $value['aname'],
            ];
        }

        $list_brand = [];
        foreach ($res_brand as $key => $value) {
            $list_brand[] = [
                "aid" => $value['aid'],
                "aname" => $value['aname'],
                "bid" => $value['bid'],
                "bname" => $value['bname'],
            ];
        }

        $this -> view -> assign('list_oem', $list_oem);
        $this -> view -> assign('list_brand', $list_brand);
        return $this->view->fetch("model_add");
    }

    public function getBrand(){
        $oem_id = $this->request->param("oem_id");
        // $map['']
        // $list = CarModel::where($map)->select();
        $res_brand = Db::query("select a.id as aid,a.name as aname "
            . "from car_model as a,car_model as b "
            . "where a.pid = b.id and b.pid = 0 and b.id = ?",[$oem_id]);

        $list_brand = [];
        foreach ($res_brand as $key => $value) {
            $list_brand[] = [
                "aid" => $value['aid'],
                "aname" => $value['aname'],
            ];
        }
        return $list_brand;

    }

    public function checkModelName(){
        $model_name = $this->request->param('name');

        // return $model_name;

        $status = 1;
        $message = '车型名称可用';
        if (empty($model_name)) {
            $status = 0;
            $message = '车型名称不能为空~~';
        };
        if (CarModel::get(['name'=> $model_name])) {
            //如果在表中查询到该用户名
            $status = 0;
            $message = '车型名称重复,请重新输入~~';
        };

        return ['status'=>$status, 'message'=>$message];

    }

    public function addModel(){
        $data = $this->request-> param();
        $status = 0;
        $message = '添加失败，车型信息不符合要求~~';

        $userr = CarModel::get(['name' => $data["model"]]);  //判断是否为注册过的用户名称
        if ($userr === null && !empty($data["model"])) {
            $user= CarModel::create(['name' => $data["model"],"pid" =>$data["brand"]]);
            if ($user) {
                $status = 1;
                $message = '添加成功~~';
            }
        }else{
            $message = '该车型名称已经注册或者为空~~';
        }

        return ['status'=>$status, 'message'=>$message];
    }

    public function modelEdit(){
        $model_id = $this->request->param('id');

        $res = Db::query("select a.id as aid,a.name as aname,"
            ."b.id as bid,b.name as bname,"
            ."c.id as cid,c.name as cname "
            . "from car_model as a,car_model as b,car_model as c "
            . "where a.pid = b.id and b.pid = c.id and a.id = ?",[$model_id]);
        
        $userName = Session::get('user_info.username');
        $map['pid'] = 0;
        if ($userName == 'admin') {
            $list_oem = CarModel::where($map)->select();
            $list_brand = Db::query("select a.id as aid,a.name as aname "
            . "from car_model as a,car_model as b "
            . "where a.pid = b.id and b.pid = 0");
        }else{
            //如果是某个厂家的，只能看到自己的
            $map['name'] = Session::get('user_info.oem');
            $oem_id = Session::get('user_info.id');
            $list_oem = CarModel::where($map)->select();
            $list_brand = Db::query("select "
                ."b.id as aid,b.name as aname "
                . "from car_model as a,car_model as b,car_model as c "
                . "where a.pid = b.id and b.pid = c.id and c.id = ?",[$oem_id]);
        }

        $ress = [];
        foreach ($res as $key => $value) {
            $ress[] = [
                "aid" => $value['aid'],
                "aname" => $value['aname'],
                "bid" => $value['bid'],
                "bname" => $value['bname'],
                "cid" => $value['cid'],
                "cname" => $value['cname'],
            ];
        }

        $list_brandd = [];
        foreach ($list_brand as $key => $value) {
            $list_brandd[] = [
                "aid" => $value['aid'],
                "aname" => $value['aname'],
            ];
        }

        $this -> view -> assign('res', $ress);
        $this -> view -> assign('list_oem', $list_oem);
        $this -> view -> assign('list_brand', $list_brandd);
        return $this->view->fetch("model_edit");

    }

    public function editModel(){
        $data = $this->request -> param();
        
        $condition = ['id'=>$data['id']] ;

        $dataa['name'] = $data['model'];
        $dataa['pid'] = $data['bid'];
        $result = CarModel::update($dataa, $condition);
        
        if (true == $result) {
            return ['status'=>1, 'message'=>'更新成功'];
        } else {
            return ['status'=>0, 'message'=>'更新失败,请检查'];
        }
    }

    public function deleteModel(){
        $oem_id = $this->request->param("id");
        CarModel::update(['is_delete'=>0],['id'=> $oem_id]);
        CarModel::destroy($oem_id);
    }

    public function hellaa(){

        $res = Db::query("select a.id as aid,a.name as aname,a.delete_time,"
            ."FROM_UNIXTIME(a.create_time,'%Y年%m月%d日') as create_time,"
            ."FROM_UNIXTIME(a.update_time,'%Y年%m月%d日') as update_time,"
            ."b.id as bid,b.name as bname,"
            ."c.id as cid,c.name as cname "
            . "from car_model as a,car_model as b,car_model as c "
            . "where a.pid = b.id and b.pid = c.id");

        $res = Db::query("select a.id as aid,a.name as aname,a.is_delete,"
            ."FROM_UNIXTIME(a.create_time,'%Y年%m月%d日') as create_time,"
            ."FROM_UNIXTIME(a.update_time,'%Y年%m月%d日') as update_time,"
            ."b.id as bid,b.name as bname,"
            ."c.id as cid,c.name as cname "
            . "from car_model as a,car_model as b,car_model as c "
            . "where a.pid = b.id and b.pid = c.id and a.is_delete = ?",[1]);
        var_dump($res);
        echo "<hr>";
        foreach ($res as $key => $value) {
            # code...
            var_dump($value['aid']);

            var_dump($value['aname']);
            var_dump($value['is_delete']);
            echo "<br/>";
        }
        echo "<hr>";
        

    }


}